package io.renren.modules.footprint.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.renren.common.annotation.SysLog;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.footprint.entity.VipfootprintinfosCustomEntity;
import io.renren.modules.projectinfo.entity.ProjectinfosEntity;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import io.renren.modules.user.service.UserinfosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.footprint.entity.VipfootprintinfosEntity;
import io.renren.modules.footprint.service.VipfootprintinfosService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author 
 * @email 
 * @date 2018-10-26 15:36:43
 */
@RestController
@RequestMapping("/footprint/vipfootprintinfos")
public class VipfootprintinfosController {
	@Autowired
	private VipfootprintinfosService vipfootprintinfosService;

	@Autowired
	private UserinfosService userinfosService;

	@Autowired
	private ProjectinfosService projectinfosService;
	
	/**
	 * 足迹列表
	 */
	@SysLog("查询足迹列表")
	@RequestMapping("/list")
	@RequiresPermissions("footprint:vipfootprintinfos:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<VipfootprintinfosCustomEntity> vipfootprintinfosList = vipfootprintinfosService.queryCustomList(query);
		int total = vipfootprintinfosService.queryCustomTotal(query);
		PageUtils pageUtil = new PageUtils(vipfootprintinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@SysLog("查询足迹信息")
	@GetMapping("/info/{vipfootprintinfoid}")
	@RequiresPermissions("footprint:vipfootprintinfos:info")
	public R info(@PathVariable("vipfootprintinfoid")String vipfootprintinfoid){
		VipfootprintinfosCustomEntity vipfootprintinfos = vipfootprintinfosService.queryCustomInfo(vipfootprintinfoid);
		return R.ok().put("vipfootprintinfos", vipfootprintinfos);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存足迹信息")
	@PostMapping("/save")
	@RequiresPermissions("footprint:vipfootprintinfos:save")
	public R save(@RequestBody VipfootprintinfosEntity vipfootprintinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(vipfootprintinfos, AddGroup.class);
		//外键判空
		//用户
		if(userinfosService.isUserIdExist(vipfootprintinfos.getUserinfoid()) == 0){
			return R.error("用户不存在");
		}
		//项目
		R projectIdExist = projectinfosService.isProjectIdExist(vipfootprintinfos.getTargetid());
		if(projectIdExist != null){
			return projectIdExist;
		}
		//是否为自己的项目，不可添加自己的项目到足迹表中
		Map<String,Object> map = new HashMap<>();
		map.put("userinfoid",vipfootprintinfos.getUserinfoid());
		map.put("projectinfoid",vipfootprintinfos.getTargetid());
		R projectExistByUser = projectinfosService.isProjectExistByUser(map);
		if(projectExistByUser == null){
			return R.error("不可添加自己的项目！");
		}
		//是否已经存在
		if(vipfootprintinfosService.judgeFoodprintExistByUser(vipfootprintinfos) == null){
			return R.error("已经存在");
		}
		vipfootprintinfosService.save(vipfootprintinfos);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改足迹信息")
	@PostMapping("/update")
	@RequiresPermissions("footprint:vipfootprintinfos:update")
	public R update(@RequestBody VipfootprintinfosEntity vipfootprintinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(vipfootprintinfos, AddGroup.class);
		//外键判空
		//用户
		if(userinfosService.isUserIdExist(vipfootprintinfos.getUserinfoid()) == 0){
			return R.ok("用户不存在");
		}
		//项目
		R projectIdExist = projectinfosService.isProjectIdExist(vipfootprintinfos.getTargetid());
		if(projectIdExist != null){
			return projectIdExist;
		}
		//是否已经存在
		if(vipfootprintinfosService.judgeFoodprintExistByUser(vipfootprintinfos) == null){
			return R.error("已经存在");
		}
		//是否为自己的项目，不可添加自己的项目到足迹表中
		Map<String,Object> map = new HashMap<>();
		map.put("userinfoid",vipfootprintinfos.getUserinfoid());
		map.put("projectinfoid",vipfootprintinfos.getTargetid());
		R projectExistByUser = projectinfosService.isProjectExistByUser(map);
		if(projectExistByUser == null){
			return R.error("不可添加自己的项目！");
		}
		vipfootprintinfosService.update(vipfootprintinfos);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除足迹信息")
	@PostMapping("/delete")
	@RequiresPermissions("footprint:vipfootprintinfos:delete")
	public R delete(@RequestBody String[]  vipfootprintinfoids){
		vipfootprintinfosService.deleteBatch( vipfootprintinfoids);
		return R.ok();
	}
	
}
